<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Vip_model extends CI_Model {

	function __construct() {
		parent::__construct();
		
	}

	/**
	 * 회원 검색 리스트
	 */
	function select_member_list($user_id) {
		log_message('debug',"#### Vip_model::select_member_list($user_id)");

		$query = $this->db->get_where('tb_member',array('user_id'=>$user_id)) or $this->logsql->log_sql('error');
		$result = $query->result_array();
		
		$this->logsql->log_sql();
		$this->logsql->log_result($result);

		return $result;
	}
	
	/**
	 * 패키지 선물
	 */
	function give_package($user_id, $target_user_id, $member_package) {
		log_message('debug',"#### Vip_model::give_package($user_id, $target_user_id, $member_package)");

		$this->db->trans_start();

		// 현재 가입중인 패키지 정보를 가져온다. ==> $member_package
		
		// 가져온 패키지정보에서 vip쿠폰 갯수를 제외하고 그대로 타겟 아이디로 복사
		$this->db->set('user_id'       , $target_user_id);
		$this->db->set('package_cd'    , $member_package['package_cd']);
		$this->db->set('vip_coupon_qty', '0');
		$this->db->set('start_dt'      , $member_package['start_dt']);
		$this->db->set('end_dt'        , $member_package['end_dt']);
		$this->db->set('giver_id'      , $user_id);
		$this->db->set('create_dt'     , 'NOW()',FALSE);
		$this->db->insert('tb_member_package');


		// 방금 입력한 받는사람의 idx 가져오기
		$query = $this->db->get_where('tb_member_package',array('user_id'=>$target_user_id,'start_dt'=>$member_package['start_dt']));
		$target_member_package = $query->row_array();
		$idx = $target_member_package['idx'];
		
		
		// package_cd로 tb_package_coupon에서 값을 가져와 타겟 아이디에 할당한다.
		$query = $this->db->get_where('tb_package_coupon', array('package_cd'=>$member_package['package_cd']));
		$package_coupon_list = $query->result_array();
		
		
		// 패키지에 해당되는 쿠폰 지급
		$member_coupon_list = array();
		for ($i=0; $i < count($package_coupon_list); $i++) { 
			$member_coupon_list[$i] = array(
				'idx'              => $idx,
				'service_group_id' => $package_coupon_list[$i]['service_group_id'],
				'qty'              => $package_coupon_list[$i]['qty']
			);
		}
		$this->db->insert_batch('tb_member_package_coupon', $member_coupon_list);

		
		// VIP이용권 하나 차감
		$this->db->set('vip_coupon_qty', 'vip_coupon_qty-1', FALSE);
		$this->db->where('idx', $member_package['idx']);
		$this->db->update('tb_member_package');

		
		// VIP이용권 선물내역을 기록한다.
		$this->db->set('user_id'    , $user_id);
		$this->db->set('receiver_id', $target_user_id);
		$this->db->set('member_package_idx' , $member_package['idx']);
		$this->db->set('package_cd' , $member_package['package_cd']);
		$this->db->set('create_dt'  , 'NOW()',FALSE);
		$this->db->insert('tb_vip_given_history');
				
		
		$this->db->trans_complete();
	}
	
	/**
	 * 회원의 사용중인 패키지 정보 가져오기
	 */
	function get_member_package($user_id) {
		log_message('debug',"#### Vip_model::get_member_package($user_id)");

		$sql  = PHP_EOL." SELECT * "; 
		$sql .= PHP_EOL." FROM  tb_member_package ";
		$sql .= PHP_EOL." WHERE DATE_FORMAT(CURDATE(),'%Y%m%d') BETWEEN start_dt AND end_dt ";
		$sql .= PHP_EOL." AND user_id = ? ";
		$query = $this->db->query($sql, array($user_id)) or $this->logsql->log_sql('error');
		$result = $query->row_array();
		
		$this->logsql->log_sql();
		$this->logsql->log_result($result);

		return $result;
	}
}

/* End of file vip_model.php */
/* Location: ./application/models/mypage/vip_model.php */